<?php
class SorderAction extends AdminPublicAction {
/*======================================================================
	 信息列表
	 ======================================================================================*/
	public function index(){
	
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "fee_order_id";
			$_GET['updown'] = $upDown = "desc";
				
		}
	
		$keyword = I('keyword','');
		
		$fee_order_status = I('fee_order_status',-1);
		$deal_status = I('deal_status',-1);
		//關鍵字查詢
		if(tep_not_null($keyword)){
			$where['products_name']  = array('like','%'.$keyword.'%');
			$where['tel']  = array('like','%'.$keyword.'%');
			$where['fee_order_num']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		$map['close_order']=0;
		//省份城市筛选
		$map = get_map_city_id($map);
		$this->city_id = $map['city_id'];
		$this->prov_id = $map['prov_id'];		
	
		$this->provcode = $cid;
		$this->citycode = $qid;
		$this->area_id = $aid;
		$fee_id = I('fee_id',-1);
		//篩選狀態
		if($fee_order_status >= 0 ) $map['fee_order_status'] = $fee_order_status;
		if($deal_status >= 0 ) $map['deal_status'] = $deal_status;
		if($fee_id >= 0 ) $map['fee_id'] = $fee_id;
	
		//$map['language_id'] = (int)L('lan_id');
	//var_dump($map);die;
		import("ORG.Util.Page");
		$page = 10;
		$count = D('Feeorder')->getOrderCount($map);//->where($map)->count();
		//echo $count;die;
		$Page = new Page($count, $page);
		$infoList = D('Feeorder')->getFeeorderList($map,$sort.' '.$upDown,$Page->firstRow. ',' . $Page->listRows);//getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//var_dump($infoList);die;
		if (S("success") != "")
		{
			$this->assign("msg", '<div class="success">'.S("success").'</div>');
			S('success',NULL);
		}
			
		if (S("err") != "")
		{
			$this->assign("msg", '<div class="err">'.S("err").'</div>');
			S('err',NULL);
		}
	
		//$this->category = $category;
		$this->fee_order_status = $fee_order_status;
		$this->deal_status = $deal_status;
		$this->fee_id = $fee_id;
		$this->keyword = $keyword;
		//$this->cate = $cate;
		$this->page = $Page->show();
		$this->infoList = $infoList;
		$this->li1 = 'current';
		$this->left_order = 'current';
		$this->display();
	}
	
	//###########################################//
	//确认订单       ，                                                                                              //
	//###########################################//
	public function confirm(){
		$editId = I('editId');
		if(!$editId){
			$this->redirect('Sorder/index');
		}
		$fee_id = M('fee_order')->where(array('fee_order_id'=>$editId))->field('fee_id,products_id')->find();
		//var_dump($fee_id);die;
		if($fee_id['fee_id']==1){
			$data1['up_flag']=1;
		}elseif($fee_id['fee_id']==2){
			$data1['jiaji_flag']=1;
		}else{
			$data1['up_flag']=1;
			$data1['jiaji_flag']=1;
		}
		$result = M('products')->where(array('products_id'=>$fee_id['products_id']))->save($data1);
		//echo $result;die;
		if($result){
			$feeday = M('fee')->where(array('fee_id'=>$fee_id['fee_id']))->field('active_day')->find();
			$data['deal_status'] = 1;
			$data['end_date'] = date('Y-m-d H:i:s',time()+$feeday['active_day']*24*3600);
			$result = M('fee_order')->where(array('fee_order_id'=>$editId))->save($data);
			if($result){
				S('success','处理订单成功!');
				$this->redirect('index');
			}else{
				S('err','处理订单失败!');
				$this->redirect('index');
			}
		}else{
			S('err','处理订单失败2!');
			$this->redirect('index');
		}
		
		
		
	}
	
	//###########################################
	//取消订单。若买家买了很久，迟迟不付款，卖家申诉，则取消订单，商品重新发布，
	//###########################################
	public function cancel(){
		$editId = I('editId');
		if(!$editId){
			$this->redirect('index');
		}
		
		$result = M('fee_order')->where(array('fee_order_id'=>$editId))->setField('close_order',1);
		if($result){
			S('success','处理订单取消成功!');			
		}else{
			S('err','处理订单取消失败!');			
		}
		$this->redirect('index');
	}
	
	/**
	 * 订单导出
	 */
	public function ToExcel(){
		if((isset($_GET['sort'] ) && !empty($_GET['sort'])) && (isset($_GET['updown'] ) && !empty($_GET['updown']))){
			$sort   = I('sort');
			$upDown = I('updown');
		}else{
			$_GET['sort'] = $sort = "fee_order_id";
			$_GET['updown'] = $upDown = "desc";
		
		}
		
		$keyword = I('keyword','');
		
		$fee_order_status = I('fee_order_status',-1);
		$deal_status = I('deal_status',-1);
		//關鍵字查詢
		if(tep_not_null($keyword)){
			$where['products_name']  = array('like','%'.$keyword.'%');
			$where['tel']  = array('like','%'.$keyword.'%');
			$where['fee_order_num']  = array('like','%'.$keyword.'%');
			$where['_logic'] = 'or';
			$map['_complex'] = $where;
		}
		$map['close_order']=0;
		//省份城市筛选
		$map = get_map_city_id($map);
		$this->city_id = $map['city_id'];
		$this->prov_id = $map['prov_id'];
		
		$this->provcode = $cid;
		$this->citycode = $qid;
		$this->area_id = $aid;
		$fee_id = I('fee_id',-1);
		//篩選狀態
		if($fee_order_status >= 0 ) $map['fee_order_status'] = $fee_order_status;
		if($deal_status >= 0 ) $map['deal_status'] = $deal_status;
		if($fee_id >= 0 ) $map['fee_id'] = $fee_id;
		
		//$map['language_id'] = (int)L('lan_id');
		//var_dump($map);die;
		$result = D('Feeorder')->getFeeorderList($map,$sort.' '.$upDown,'');//getAll($map,$Page->firstRow. ',' . $Page->listRows,$sort.' '.$upDown);
		//var_dump($result);die;
		$xls_name = "置顶交易列表";
		header("Content-type:application/vnd.ms-excel;charset=UTF-8");
		header("Content-Disposition:attachment;filename=$xls_name.xls");
		
		$table = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel"	xmlns="http://www.w3.org/TR/REC-html40">
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
            <html>
            <head>
            <meta http-equiv="Content-type" content="text/html;charset=UTF-8" />
            <style id="Classeur1_16681_Styles"></style>
            </head>';
		$table .= '<table style="border:1px solid #000; border-spacing:0px; width:1000px;text-align:left;border-collapse: collapse;">';
		$table .= '<thead><tr style="background:#c6c6c6;height:40px;"><th style="border:1px solid #000;">订单编号</th><th style="border:1px solid #000;">时间</th><th style="border:1px solid #000;">商品名称</th><th style="border:1px solid #000;">用户</th><th style="border:1px solid #000;">电话</th><th style="border:1px solid #000;">套餐</th><th style="border:1px solid #000;">金额</th><th style="border:1px solid #000;">支付状态</th><th style="border:1px solid #000;">操作状态</th><th style="border:1px solid #000;">订单过期</th></thead>';
		$table .= '<tbody>';
		foreach($result as $key=>$value){
			if(($key+1)%2==0){
				$table .= "<tr style='background:#a9c6c9'>";
			}else{
				$table .= "<tr>";
			}
		
			$table .= "<td style='border:1px solid #000;'>".$value['fee_order_num']."</td>";
			$table .= "<td style='border:1px solid #000;'>".$value['add_date'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['products_name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['member_name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['tel'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['fee_name'].'</td>';
			$table .= "<td style='border:1px solid #000;'>".$value['fee_money'].'</td>';
			if($value['fee_order_status']==1){
				$table .= "<td style='border:1px solid #000;'>已支付</td>";
			}elseif($value['fee_order_status']==0){
				$table .= "<td style='border:1px solid #000;'>未支付</td>";
			}
			if($value['deal_status']==1){
				$table .= "<td style='border:1px solid #000;'>已确认</td>";
			}elseif($value['deal_status']==0){
				$table .= "<td style='border:1px solid #000;'>未确认</td>";
			}
			if($value['fee_status']==1){
				$table .= "<td style='border:1px solid #000;'>已到期</td>";
			}elseif($value['fee_status']==0){
				$table .= "<td style='border:1px solid #000;'>未过期</td>";
			}
			$table .= '</tr>';
		}
		$table .= '</tbody>';
		$table .= '</table>';
		echo $table;
	}
}